package com.weavernorth.sysap.checkstatus;


import com.weavernorth.util.Logger4jUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * @program: ecology_erdos
 * @description: 检测接口工具类
 * @author: zm
 * @create: 2019-12-09 13:44
 **/


public class CheckUtil {
//    public static Log log = LogFactory.getLog(CheckUtil.class.getName());
    private Logger log = Logger4jUtil.getLoggerByName("/Timmer/synchronization_log/");
    /**
     * 分部表未被封存的数据
     */

    public String getHrmsubcompanyXml(String strTablename, String strCode) {

        //数据库操作
        RecordSet rsSelect = new RecordSet();
        //返回xml
        StringBuffer strResult = new StringBuffer();

        //1.查询分部,部门表未被封存的
        StringBuffer subSqlBuffer = new StringBuffer();
        subSqlBuffer.append("select " + strCode + "  from " + strTablename + " where ");


        if (!"HrmJobtitles".equalsIgnoreCase(strTablename) && !"hrmresource".equalsIgnoreCase(strTablename)) {
            subSqlBuffer.append("(canceled  is null or canceled=0 )");
        } else if ("hrmresource".equalsIgnoreCase(strTablename)) {
           // subSqlBuffer.append(" status<4 and  (accounttype!=1 or accounttype is null)");
             subSqlBuffer.append(" status<7 and  (accounttype!=1 or accounttype is null)");
        } else if ("HrmJobtitles".equalsIgnoreCase(strTablename)) {
            subSqlBuffer.append("(jobstatus  is null  or jobstatus=1)");
        }
        log.info("检测接口查询数据" + subSqlBuffer.toString());
        rsSelect.executeQuery(subSqlBuffer.toString());
        while (rsSelect.next()) {
            //编码
            String strCurrCode = Util.null2String(rsSelect.getString(strCode));
            if (!"".equals(strCurrCode)) {
                if (strCurrCode.indexOf("_") == (-1)) {
                    strResult.append("<item>");
                    strResult.append("<OBJID>" + Util.null2String(rsSelect.getString(strCode)) + "</OBJID>");
                    strResult.append("<ZFW_T>" + strTablename + "</ZFW_T>");
                    strResult.append("</item>");
                }
            }
        }
        return strResult.toString();

    }

    /**
     * 检测接口后更新状态,sap返回离职,禁用,封存等数据
     *
     * @param strTable OA表名;strCode 编码,strLzrq 离职日期
     */

    public void updateStatus(String strTable, String strCode,String strName,String strLzrq) {
        //数据库操作
        RecordSet rsUpdate = new RecordSet();
        //更新字段名
        String strFieldName = "";
        //编码字段名
        String strCodename = "";
        //根据表名判断更新字段值
        if ("hrmresource".equalsIgnoreCase(strTable)) {
            strFieldName = "status='5',loginid=null,enddate='"+strLzrq+"',lastname='"+strName+"'";
            strCodename = "workcode";
        } else if ("hrmsubcompany".equalsIgnoreCase(strTable)) {
            //1为封存
            strFieldName = "canceled='1'";
            strCodename = "subcompanycode";
        } else if ("Hrmdepartment".equalsIgnoreCase(strTable)) {
            strFieldName = "canceled='1'";
            strCodename = "departmentcode";
        } else if ("HrmJobtitles".equalsIgnoreCase(strTable)) {
            //0为 禁用
            strFieldName = "jobstatus='0'";
            strCodename = "jobtitlecode";
        }
        //执行更新操作

        String updateSql="update " + strTable + " set " + strFieldName + " where " + strCodename + " ='" + strCode+"'";
        log.info("检测接口更新操作:"+updateSql);
        rsUpdate.executeUpdate(updateSql);


    }
}
